package com.yanqu.road.server.manger.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ChannelActivityBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.TempMgr;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 所有活动的info
 */
public class ActivityInfoMgr extends TempMgr {


    private static final Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();


    public static ActivityInfo getActivityInfo(int activityId) {
        if (activityId <= 0) {
            return null;
        }

        if (!activityInfoMap.containsKey(activityId)) {
            synchronized (activityInfoMap) {

                // 从中心库读
                if (!activityInfoMap.containsKey(activityId)) {
                    ActivityInfo info = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (info != null) {
                        activityInfoMap.put(info.getActivityId(), info);
                    }
                }

                // 从跨地区中心库读
                if (!activityInfoMap.containsKey(activityId)) {
                    String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
                    ActivityInfo info = ChannelActivityBussiness.getActivityInfoById(dbParam, activityId, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
                    if (info != null) {
                        activityInfoMap.put(activityId, info);
                    }
                }

                // 异常日志
                if (!activityInfoMap.containsKey(activityId)) {
                    getLogger().error("can not find activityInfo!!! activityId = {}", activityId);
                }
            }
        }
        return activityInfoMap.get(activityId);
    }


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
